﻿/**
 * @author Namla
 */
$('#supportLink').click(function (e) {
           e.preventDefault();
           if ($(this).hasClass('disabled')){
                $(this).removeClass('disabled');
                return false; // Do something else in here if required
            }
               
            else{
                $(this).addClass('disabled');
                window.location.href = $(this).attr('href');
            }         
        }); 
function comment(product_id,user_id){
	var comment=$("#comment").val();
	var count= Number($("#count_comment").val());
	var arr =  $(".star-rating-on");
	var star= arr.length;
	if(comment==' ' || comment==0) alert("Nội dung nhập vào không chính xác!");
	else{
		$.ajax({
			url: '/default/index/comment',
			data: 'product_id= '+product_id+"&user_id="+user_id+"&comment="+comment+"&star="+star+"&count="+count,
			type: 'POST',
			success: function(data){
				if(count==0) $("#count_comment").after(data);
				else $("#comment_more").before(data);
				$("#count_comment").val((1+count));
				$("#comment").val('');
			}
		});
		
	}
}

function moreProduct(page,category,url,tag){
    $.ajax({
       url: url,
       data: {page:page, category:category, tag:tag},
       type: 'GET' ,
       success: function(data){
            $('#product_more').before(data);
            $('.seemore:first').remove();
            $('#product_more:first').remove();
       }
    });
}

function moreSearch(page,search_key,url){
    $.ajax({
       url: url,
       data: {page:page, search_key:search_key},
       type: 'GET' ,
       success: function(data){
            $('#product_more').before(data);
            $('.seemore:first').remove();
            $('#product_more:first').remove();
       }
    });
}
function moreComment(page,url){
    $.ajax({
        url: url,
        data: "page="+page+"&more=1",
        type:'POST',
        success:function(data){
            $('.comment').before(data);
            $('.seemore:first').remove();
            $('#comment_more:first').remove();
        },
        error:function(data){
            alert("error");
        }
    });
}
function load_bottom(object_off,url,object_on,url2){
    $("#bottom_header").load(url);
    $("."+object_off).attr('onclick',"hide_bottom('"+object_off+"','"+url+"','"+object_on+"','"+url2+"')");
    $("."+object_on).attr('onclick',"load_bottom('"+object_on+"','"+url2+"','"+object_off+"','"+url+"')");
}
function hide_bottom(object_off,url,object_on,url2){
    $("#bottom_header").html("");
    $("."+object_off).attr('onclick',"load_bottom('"+object_off+"','"+url+"','"+object_on+"','"+url2+"')");
}
function load_bottom2(url){
    $("#bottom_header").load(url);
}
function submitForm(id){
    $("#"+id).submit();
}
function checkLogin(){
    var name=$("#user_name").val();
    var pass=$("#pass").val();
    if( name=="" || name==0){
        alert("Tên đăng nhập không chính xác");
        $("#user_name").focus();
        return false;
    }
    if( pass =="" || pass==0){
        alert("Mật khẩu không được để rỗng");
        $("#pass").focus();
        return false;
    }
    $.ajax({
        url:"/index/login",
        type:"POST",
        data: "user_name="+name+"&pass="+pass,
        success: function(data){
            var r=data.split("|");
            
            if(r[0]==1){
                window.location.reload( true );
                //$(".message").html(data);
            }
            else{
                $(".message").html(r[1]);
                return false;
            }
        },
        error:function(){
            alert("ERROR");
            return false;
        }
        
    });
    return false;
}
function checkRegister(){
    var user_name= $("#user_name").val();
    var pass=$("#pass").val();
    var re_pass=$("#retype_pass").val();
    var gender=document.getElementsByName("gender");
    if(gender[0].checked==true) gender="1";else gender=0;
    if(user_name==0||user_name==''){
        alert("Tên không chính xác");
        $("#user_name").focus();
        return false;
    }
    if(pass==0||pass==''){
        alert("Mật khẩu không được để rỗng");
        $("#pass").focus;
        return false;
    }
    $.ajax({
        url:"/index/register",
        type:"POST",
        data:"user_name="+user_name+"&pass="+pass+"&retype_pass="+re_pass+"&gender="+gender,
        success:function(data){
            var r=data.split("|");
            if(r[0]==1){
                window.location="/index";
            }
            else{
                $(".message").html(r[1]);
                return false;
            }
        },
        error:function(){
            alert("Error loading from sever");
            return false;
        }
    })
    return false;
}

function del_input(id){
    document.getElementById(id).value = "";
}
// display menu top
function display_hover(){
    if($("#menu_hover").css("display") == "block")
        $("#menu_hover").css("display", "none");
    if($("#menu_hover").css("display") == "none")
        $("#menu_hover").css("display", "block");
}
// add like
function addLike(){
		      var data = "id="+$('#id_product').val();
		  	  var url = "/default/index/addlike";	    
			  	 	$.ajax({ 
			  	    	url : url,
			  	    	type: 'POST',
			  	    	cache : false,
			  	    	data : data,
			  	    	success:function(html){
			  	    	     if(html == "error"){
			  	    	        alert("Bạn chỉ có thể nhấn like 30 phút một lần"); 
			  	    	     }else{
			  	    			$(".like_span").html('('+html+')');                              
                                }
			  	    	},
			  	    	error : function (data){ 
			  	    		alert(data);
			  	    	}
			  	    });
	}
// add like
function addShare(){
		      var data = "id="+$('#id_product').val();
		  	  var url = "/default/index/addshare";	    
			  	 	$.ajax({ 
			  	    	url : url,
			  	    	type: 'POST',
			  	    	cache : false,
			  	    	data : data,
			  	    	success:function(html){
			  	    			$(".share_span").html('('+html+')');
                                $("#product_shares").val(html);
			  	    	},
			  	    	error : function (data){ 
			  	    		alert(data);
			  	    	}
			  	    });
}

    
// display menu when hover
function display(div){
    $("#"+div).css("display", "block");
}
function disable(div){
    $("#"+div).css("display", "none");
}

function loadAjax(link,type){
    $.ajax({
       url:link,
       type:"POST",
       data:"test=1",
       success:function(data){      
            $("#loadAjax").html(data);
            var star=$("#star").val();  
            $("#rating :radio.star").rating();
            $(".link_info").css('color','black');
            $(".link_vote").css('color','black');
            $(".link_high").css('color','black');
            if(type==1) $(".link_info").css("color","#83A913");
            if(type==2) $('.link_vote').css("color","#83A913");
            if(type==3) $('.link_high').css("color","#83A913");
            $('#slider-one').movingBoxes({
        		startPanel   : 0,      // start with this panel
        		reducedSize  : 0.8,    // non-current panel size: 80% of panel size
        		wrap         : true,   // if true, the panel will "wrap" (it really rewinds/fast forwards) at the ends
        		buildNav     : true,   // if true, navigation links will be added
        		navFormatter : function(){ return "&#9679;"; }, // function which returns the navigation text for each panel
                initAnimation: false
        		// width and panelWidth options removed in v2.2.2, but still backwards compatible
        		// width        : 300,    // overall width of movingBoxes (not including navigation arrows)
        		// panelWidth   : 0.5,    // current panel width

            });
        },
       error:function(){
        alert("Error loading from sever");
       }
    });
}
function menu_hover(){
    $("#menu_hover").css("z-index","999");
    var display=$("#menu_hover").css("display").toString();
    if(display=="block") $("#menu_hover").css("display","none");
    else $("#menu_hover").css("display","block");
        
}